echo "===================== OpenStack Deploy Model ======================="
echo "                                                                    "
echo "                      (Internet)      (Managment Network)           "
echo "                          |                     |                   "
echo "                    -------------+--           ---+------------     "
echo "                                 |                |                 "
echo "                             +--eth1-------------eth0---+           "
echo "                             |                          |           "
echo "                             |  Nova-API, Nova-Conductor|           "
echo "                             |  Nova Scheduler          |           "
echo "                             |  Neutron Server          |           "
echo "                             |  Cinder-API,Cinder-volume|           "
echo "                             +--------------------------+           "
echo "                                                                    "
echo "                                                                    "
echo "Usage:openstack_install.sh <managemnt IP>                           "
echo "      ex) openstack_install.sh 192.168.1.1                          "
echo "Are you specify host IP($1)?                                        "
echo "===================================================================="

sleep 5
echo "The number of arguments is: $#"

if [ $# -eq 1 ]
then
        echo "success"
else
        echo "fail"
        exit
fi

function initial_config() 
{
	echo "going..."
	apt-get update

	msg="s/127.0.1.1/$1/g"
	sed -i $msg /etc/hosts

	echo "
	net.ipv4.ip_forward=1
	net.ipv4.conf.all.rp_filter=0
	net.ipv4.conf.default.rp_filter=0
	" >> /etc/sysctl.conf
}

function install_db()
{
	echo "Install MySQL Server"
	sudo apt-get install -q -y python-mysqldb mysql-server

	rm /etc/mysql/my.cnf
	wget -O /etc/mysql/my.cnf https://conf.googlecode.com/svn/trunk/openstack/icehouse/etc/mysql/my.cnf
	msg="s/%SERVER%/$1/g"
	sudo sed -i $msg /etc/mysql/my.cnf

	sudo service mysql restart
	mysql_install_db
	mysql_secure_installation
}


function drop_db()
{
	echo "Drop OpenStack DB"
mysql -u root -p  <<EOF
DROP DATABASE nova;
DROP DATABASE glance;
DROP DATABASE keystone;
DROP DATABASE neutron;
DROP DATABASE cinder;
FLUSH PRIVILEGES;
EOF
}

function update_db()
{
	echo "Update OpenStack DB"
mysql -u root -p <<EOF
CREATE DATABASE nova;
GRANT ALL PRIVILEGES ON nova.* TO 'nova'@'%' \
IDENTIFIED BY 'NOVA_DBPASS';
CREATE DATABASE glance;
GRANT ALL PRIVILEGES ON glance.* TO 'glance'@'%' \
IDENTIFIED BY 'GLANCE_DBPASS';
CREATE DATABASE keystone;
GRANT ALL PRIVILEGES ON keystone.* TO 'keystone'@'%' \
IDENTIFIED BY 'KEYSTONE_DBPASS';
CREATE DATABASE neutron;
GRANT ALL PRIVILEGES ON neutron.* TO 'neutron'@'%' \
IDENTIFIED BY 'NEUTRON_DBPASS';
CREATE DATABASE cinder;
GRANT ALL PRIVILEGES ON cinder.* TO 'cinder'@'%' \
IDENTIFIED BY 'CINDER_DBPASS';
GRANT ALL PRIVILEGES ON nova.* TO 'nova'@'localhost' \
IDENTIFIED BY 'NOVA_DBPASS';
GRANT ALL PRIVILEGES ON glance.* TO 'glance'@'localhost' \
IDENTIFIED BY 'GLANCE_DBPASS';
GRANT ALL PRIVILEGES ON keystone.* TO 'keystone'@'localhost' \
IDENTIFIED BY 'KEYSTONE_DBPASS';
GRANT ALL PRIVILEGES ON neutron.* TO 'neutron'@'localhost' \
IDENTIFIED BY 'NEUTRON_DBPASS';
GRANT ALL PRIVILEGES ON cinder.* TO 'cinder'@'localhost' \
IDENTIFIED BY 'CINDER_DBPASS';
FLUSH PRIVILEGES;
EOF
}

function install_mq()
{
	echo "Install Rabbit Server"
	sudo apt-get install -y rabbitmq-server
	sudo rabbitmqctl change_password guest RABBIT_PASS
}
 

function install_keystone()
{
	echo "Install Keystone"
	sudo apt-get install -y keystone python-keystone python-keystoneclient

	FILE="/etc/keystone/keystone.conf"
	rm -f $FILE
	wget -O $FILE https://conf.googlecode.com/svn/trunk/openstack/icehouse/$FILE
	msg="s/%SERVER%/$1/g"
	sudo sed -i $msg $FILE

	su -s /bin/sh -c "keystone-manage db_sync" keystone

	service keystone restart
}

function update_keystone()
{
	echo "Update openrc"
	wget -O /root/openrc https://conf.googlecode.com/svn/trunk/openstack/icehouse/openrc
	msg="s/%SERVER%/$1/g"
	sudo sed -i $msg /root/openrc
	source ~/openrc
	echo "source ~/openrc" >> ~/.bashrc

	echo "DEBUG..."
	echo $OS_SERVICE_TOKEN
	echo $OS_SERVICE_ENDPOINT
	echo "END debug..."
	sleep 5

	echo "Update keystone-init"
	rm -f /root/keystone-init
	wget -O /root/keystone-init https://conf.googlecode.com/svn/trunk/openstack/icehouse/keystone-init
	sudo sed -i $msg /root/keystone-init

	chmod 755 ~/keystone-init
	~/keystone-init
}

function install_glance()
{
	echo "Install glance"
	sudo apt-get install -y glance glance-api glance-registry python-glanceclient glance-common
	rm /etc/glance/glance-api.conf
	wget -O /etc/glance/glance-api.conf https://conf.googlecode.com/svn/trunk/openstack/icehouse/etc/glance/glance-api.conf
	msg="s/%SERVER%/$1/g"
	sudo sed -i $msg /etc/glance/glance-api.conf

	rm /etc/glance/glance-registry.conf
	wget -O /etc/glance/glance-registry.conf https://conf.googlecode.com/svn/trunk/openstack/icehouse/etc/glance/glance-registry.conf
	sudo sed -i $msg /etc/glance/glance-registry.conf

	su -s /bin/sh -c "glance-manage db_sync" glance

	sudo service glance-api restart && sudo service glance-registry restart

}

function install_nova()
{
	sudo apt-get install -y nova-api nova-cert nova-common nova-conductor nova-scheduler python-nova python-novaclient nova-consoleauth novnc nova-novncproxy 

	FILE=/etc/nova/nova.conf
	rm -f $FILE
	wget -O $FILE https://conf.googlecode.com/svn/trunk/openstack/icehouse$FILE
	msg="s/%SERVER%/$1/g"
	sudo sed -i $msg $FILE

	FILE=/etc/nova/api-paste.ini
	rm -f $FILE
	wget -O $FILE https://conf.googlecode.com/svn/trunk/openstack/icehouse$FILE
	msg="s/%SERVER%/$1/g"
	sudo sed -i $msg $FILE


	rm -f /var/lib/nova/nova.sqlite
	su -s /bin/sh -c "nova-manage db sync" nova

	echo "Restart nova service...."	
	service nova-api restart
	service nova-cert restart
	service nova-consoleauth restart
	service nova-scheduler restart
	service nova-conductor restart

}

function install_neutron()
{
	echo "Install Neutron"
	sudo apt-get install -y openvswitch-controller openvswitch-switch openvswitch-datapath-source
	sudo apt-get install -y neutron-server neutron-dhcp-agent neutron-plugin-openvswitch-agent neutron-l3-agent
	rm -f /etc/neutron/neutron.conf
	wget -O /etc/neutron/neutron.conf https://conf.googlecode.com/svn/trunk/openstack/icehouse/etc/neutron/neutron.conf
	msg="s/%SERVER%/$1/g"
	sudo sed -i $msg /etc/neutron/neutron.conf


	rm -f /etc/neutron/dhcp_agent.ini
	wget -O /etc/neutron/dhcp_agent.ini https://conf.googlecode.com/svn/trunk/openstack/icehouse/etc/neutron/dhcp_agent.ini
	rm -f /etc/neutron/l3_agent.ini
	wget -O /etc/neutron/l3_agent.ini https://conf.googlecode.com/svn/trunk/openstack/icehouse/etc/neutron/l3_agent.ini

	rm -f /etc/neutron/dnsmasq-neutron.ini
	wget -O /etc/neutron/dnsmasq-neutron.ini https://conf.googlecode.com/svn/trunk/openstack/icehouse/etc/neutron/dnsmasq-neutron.ini


	rm /etc/neutron/plugins/ml2/ml2_conf.ini
	wget -O /etc/neutron/plugins/ml2/ml2_conf.ini https://conf.googlecode.com/svn/trunk/openstack/icehouse/etc/neutron/plugins/ml2/ml2_conf.ini


        echo "Restart nova service...."
        service nova-api restart
        service nova-cert restart
        service nova-consoleauth restart
        service nova-scheduler restart
        service nova-conductor restart

	echo "Update admin_tenant=<service tenant UUID> in /etc/neutron/neuton.conf"
	service neutron-server restart 
	service neutron-plugin-openvswitch-agent restart
	service neutron-dhcp-agent restart
	service neutron-l3-agent restart

}

host=$1
initial_config $host
install_db $host
#drop_db
update_db
install_mq
install_keystone $host
update_keystone $host
install_glance $host
install_nova $host
install_neutron $host
